package com.bootdo.common.service;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.*;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * JPushService
 *
 * @author chang
 * @version 1.0.0
 * @since 2018年8月27日 09:36
 **/
@Service
public class JPushService {
    private static Logger logger = LoggerFactory.getLogger(JPushService.class);

    //正常app
    private static final String APP_KEY = "81d995fd19cc222af58687f9";
    private static final String MASTER_SECRET = "8a7e2dc26d1b22de8ffb005f";


    /**
     * 推送所有人
     * @param content 推送的内容
     * @param type 状态
     * @return
     */
    public void allPush(String content,Integer type, String id) {
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());

        // For push, all you need do is to build PushPayload object.
        PushPayload payload = buildPushObject_all_registrationId_alert(content,type,id);

        try {
            PushResult result = jpushClient.sendPush(payload);
            logger.info("Got result - " + result);

        } catch (APIConnectionException e) {
            // Connection error, should retry later
            logger.error("Connection error, should retry later", e);

        } catch (APIRequestException e) {
            // Should review the error, and fix the request
            logger.error("Should review the error, and fix the request", e);
            logger.info("HTTP Status: " + e.getStatus());
            logger.info("Error Code: " + e.getErrorCode());
            logger.info("Error Message: " + e.getErrorMessage());
        }finally {
            jpushClient.close();
        }
    }

    /**
     * 推送指定的人
     * @param list 指定的人
     * @param content 推送的内容
     * @param type 状态0
     * @return
     */
    public void checkPush(List<String> list,String content,Integer type,String id) {
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());

        // For push, all you need do is to build PushPayload object.
        PushPayload payload = buildPushObject_someOne_registrationId_alert(list,content,type,id);

        try {
            PushResult result = jpushClient.sendPush(payload);
            logger.info("Got result - " + result);

        } catch (APIConnectionException e) {
            // Connection error, should retry later
            logger.error("Connection error, should retry later", e);

        } catch (APIRequestException e) {
            // Should review the error, and fix the request
            logger.error("Should review the error, and fix the request", e);
            logger.info("HTTP Status: " + e.getStatus());
            logger.info("Error Code: " + e.getErrorCode());
            logger.info("Error Message: " + e.getErrorMessage());
        }finally {
            jpushClient.close();
        }
    }

    /**
     * 推送指定的人
     * @param list 指定的人
     * @param content 推送的内容
     * @param type 状态0
     * @return
     */
    public void cabPush(List<String> list,String content,Integer type) {
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
        // For push, all you need do is to build PushPayload object.
        PushPayload payload = custom_android_message(list,content,type);
        try {
            PushResult result = jpushClient.sendPush(payload);
            logger.info(",Got result - " + result);

        } catch (APIConnectionException e) {
            // Connection error, should retry later
            logger.error("Connection error, should retry later", e);

        } catch (APIRequestException e) {
            // Should review the error, and fix the request
            logger.error("Should review the error, and fix the request", e);
            logger.info("HTTP Status: " + e.getStatus());
            logger.info("Error Code: " + e.getErrorCode());
            logger.info("Error Message: " + e.getErrorMessage());
        }finally {
            jpushClient.close();
        }
    }






    /**
     * 构建推送对象：所有平台，推送目标是所有人，通知内容为content。
     * @return
     */
    public static PushPayload buildPushObject_all_registrationId_alert(String content,Integer type, String id) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()    //ios
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(content)
                                .setBadge(0)
                                .addExtra("type", type)
                                .addExtra("id", id)
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(content)
                                .addExtra("type", type)
                                .addExtra("id", id)
                                .build())
                        .build())
                .build();
    }

    /**
     * 构建推送对象：所有平台，推送目标是指定的人，通知内容为 content。
     * @return
     */
    public static PushPayload buildPushObject_someOne_registrationId_alert(List<String> list, String content,Integer type,String id) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(list))  //alias是别名   registrationId是设备标识
                .setNotification(Notification.newBuilder()    //ios
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(content)
                                .setBadge(0)
                                .addExtra("type", type)
                                .addExtra("id", id)
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(content)
                                .addExtra("type", type)
                                .addExtra("id", id)
                                .build())
                        .build())
                .build();
    }

    /**
     * 构建推送对象：android设备 柜子上的，推送目标是指定的人，通知内容为 content。
     * @return
     */
    public static PushPayload custom_android_message(List<String> list, String content,Integer type) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.registrationId(list))
                .setMessage(Message.newBuilder()
                        .setMsgContent(content)
                        .addExtra("type", type)
                        .build())
                .build();
    }


    /**
     * 构建推送对象：android设备 柜子上的，音频通话服务 推送目标是指定的人，通知内容为 content。
     * @return
     */
    public static PushPayload custom_android_call_message(List<String> list, String content,Integer type, String userId, String token) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.registrationId(list))
                .setMessage(Message.newBuilder()
                        .setMsgContent(content)
                        .addExtra("type", type)
                        .addExtra("userId", userId)
                        .addExtra("token", token)
                        .build())
                .build();
    }


    /**
     * 构建推送对象：android设备 柜子上的，音频通话服务 推送目标是指定的人，通知内容为 content。
     * @return
     */
    public static PushPayload custom_all_call_message(List<String> list, String content,Integer type) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.registrationId(list))
                .setMessage(Message.newBuilder()
                        .setMsgContent(content)
                        .addExtra("type", type)
                        .build())
                .build();
    }


}
